package cn.jussi.mvcproject.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {
	/**
	 * 创建c3p0的连接池
	 */
	private static DataSource dataSource = null;
	static {	//静态代码块只执行一次
		dataSource = new ComboPooledDataSource("mssql");
	}
	
	/**
	 * 获取到数据库 SQLSever 的数据连接对象
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 通用的连接关闭方法
	 * @param conn
	 */
	public static void closeConn(Connection conn) {
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 通用的事务回滚
	 * @param conn
	 */
	public static void rollbackTransation(Connection conn) {
		if(conn != null) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
